<?php


namespace app\extra\WeixinSDK\V3;

/**
 * 统一支付接口类
 */
class UnifiedOrderpub extends Wxpayclientpub {

    function __construct() {
        //设置接口链接
        $this->url          = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        //设置curl超时时间
        $this->curl_timeout = WxPayConfpub::CURL_TIMEOUT;
    }

    /**
     * 生成接口参数xml
     */
    function createXml() {
        //检测必填参数
        if ($this->parameters["out_trade_no"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no！" . "<br>");
        } elseif ($this->parameters["body"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数body！" . "<br>");
        } elseif ($this->parameters["total_fee"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee！" . "<br>");
        } elseif ($this->parameters["notify_url"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url！" . "<br>");
        } elseif ($this->parameters["trade_type"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type！" . "<br>");
        } elseif ($this->parameters["trade_type"] == "JSAPI" &&
                $this->parameters["openid"] == NULL) {
            throw new SDKRuntimeException("统一支付接口中，缺少必填参数openid！trade_type为JSAPI时，openid为必填参数！" . "<br>");
        }


        $this->parameters["appid"]            = WxPayConfpub::APPID; //公众账号ID
        $this->parameters["mch_id"]           = WxPayConfpub::MCHID; //商户号
        $ip                                   = explode(',', $_SERVER['REMOTE_ADDR']);
        $this->parameters["spbill_create_ip"] = $ip[0]; //终端ip	    
        $this->parameters["nonce_str"]        = time(); //随机字符串


        $this->parameters["sign"] = $this->getSign($this->parameters); //签名
        ksort($this->parameters);
        return $this->arrayToXml($this->parameters);
    }

    /**
     * 红包生成接口参数xml
     */
    function createXmlred() {
        //检测必填参数
        if ($this->parameters["mch_billno"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no！" . "<br>");
        }

        $this->parameters["wxappid"]   = WxPayConfpub::APPID; //公众账号ID
        $this->parameters["mch_id"]    = WxPayConfpub::MCHID; //商户号
        $ip                            = explode(',', $_SERVER['REMOTE_ADDR']);
        $this->parameters["client_ip"] = $ip[0]; //终端ip	    
        $this->parameters["nonce_str"] = time(); //随机字符串 
        $this->parameters["sign"]      = $this->getSign($this->parameters); //签名

        return $this->arrayToXml($this->parameters);
    }

    /**
     * 生成接口参数xml
     */
    function createXmlapp() {
        //检测必填参数
        if ($this->parameters["out_trade_no"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no！" . "<br>");
        } elseif ($this->parameters["body"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数body！" . "<br>");
        } elseif ($this->parameters["total_fee"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee！" . "<br>");
        } elseif ($this->parameters["notify_url"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url！" . "<br>");
        } elseif ($this->parameters["trade_type"] == null) {
            throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type！" . "<br>");
        }
        $this->parameters["appid"]            = WxPayConfpubapp::APPID; //公众账号ID
        $this->parameters["mch_id"]           = WxPayConfpubapp::MCHID; //商户号 
	 
        $this->parameters["spbill_create_ip"] =$this->get_client_ip(); //终端ip	    
        $this->parameters["nonce_str"]        = time(); //随机字符串


        $this->parameters["sign"] = $this->getSignapp($this->parameters); //签名 
        ksort($this->parameters);
        return $this->arrayToXml($this->parameters);
    }


public function get_client_ip(){    
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
	 
    return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
 
 }
    /**
     * 获取prepay_id
     */
    function getPrepayId() {

        $this->postXml();
        $this->result = Commonutilpub::xmlToArray($this->response); 
		if(!isset($this->result["prepay_id"])){		
		 exception($this->result["err_code_des"], 1000111);
		}
        $prepay_id    = $this->result["prepay_id"];
        return $prepay_id;
    }

    /**
     * 获取prepay_id
     */
    function getPrepayIdapp() {

        $this->postXmlapp();
        $this->result = $this->response;
        return $this->result;
    }

}
